package com.Shop.Center.controller;

import com.Shop.Center.Config.AlipayConfig;
import com.Shop.Center.entity.User;
import com.Shop.Center.service.*;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@org.springframework.stereotype.Controller
@CrossOrigin
public class Controller {
    @Autowired
    private ItemsService itemsService;
    @Autowired
    private ShopListService shopListService;
    @Autowired
    private NewsService newsService;
    @Autowired
    private NoticeService noticeService;
    @Autowired
    private QuestionsService questionsService;
    @Autowired
    private DistributeService distributeService;
    @Autowired
    private UserService userService;
    @Autowired
    private Login login;
    @Autowired
    private Logout logout;

    @ResponseBody
    @PostMapping("/items")
    public List getItems(@RequestParam("type") String type) {
        return itemsService.get(type);
    }

    @ResponseBody
    @GetMapping("/list")
    public List getList(){
        return shopListService.get();
    }

    @ResponseBody
    @GetMapping("/news")
    public List getNews(){
        return newsService.get();
    }

    @ResponseBody
    @GetMapping("/notice")
    public List getNotice(){
        return noticeService.get();
    }

    @ResponseBody
    @GetMapping("/questions")
    public List getQuestion(){
        return questionsService.get();
    }

    @ResponseBody
    @GetMapping("/distribute")
    public List getDistribute(){
        return distributeService.get();
    }

    @ResponseBody
    @PostMapping("/search")
    public List getSearch(@RequestParam("name") String name){
        return itemsService.search(name);
    }

    @ResponseBody
    @PostMapping("/regist")
    public int runRegist(@RequestParam("name") String name, @RequestParam("age") String age,
                         @RequestParam("telephone") String telephone, @RequestParam("password") String password,
                         @RequestParam("destination") String destination){
        return userService.insert(name,age,telephone,password,destination);
    }

    @ResponseBody
    @PostMapping("/login")
    public Boolean runLogin(@RequestParam("name") String name, @RequestParam("password") String password){
        return login.user_login(name, password);
    }

    @ResponseBody
    @PostMapping("/test")
    public void tst(@RequestParam("nb") String a){
        logout.out(a);
    }

    @ResponseBody
    @GetMapping("/is_login")
    public Boolean getLogin(){
        return Login.is_login;
    }

    @ResponseBody
    @PostMapping("/user")
    public List<User> get_user(@RequestParam("userName") String userName){
        return userService.get(userName);
    }

    @ResponseBody
    @PostMapping("/update_user")
    public int updateUser(@RequestParam("name") String name, @RequestParam("age") String age, @RequestParam("telephone") String telephone, @RequestParam("destination") String destination, @RequestParam("points") String points, @RequestParam("id") int id){
        return userService.update(name, age, telephone, destination, points, id);
    }

    @Autowired
    EvaluateService evaluateService;

    @ResponseBody
    @PostMapping("/add_evaluate")
    public int addEvaluate(@RequestParam("itemid") long itemid, @RequestParam("content") String content, @RequestParam("userid") long userid){
        if(Login.is_login){
            return evaluateService.add(itemid,content,userid);
        }
        return 0;
    }

    @Autowired
    ShoppingCartService shopcarService;

    @ResponseBody
    @PostMapping("/add_caritem")
    public int addCarItem(@RequestParam("itemid") long itemid, @RequestParam("count") int count,
                          @RequestParam("userid") long userid, @RequestParam("img")String img,
                          @RequestParam("goodsName") String goodsName, @RequestParam("sellingPrice")Double sellingPrice){
        return shopcarService.add(itemid,count,userid,img,goodsName,sellingPrice);
    }
    @ResponseBody
    @PostMapping("/delete_caritem")
    public int deleteCarItem(@RequestParam("id") long id){
        return shopcarService.delete(id);
    }

    @ResponseBody
    @PostMapping("/get_caritem")
    public List getCarItem(@RequestParam("userid") long userid){
        return shopcarService.getMyShoppingCartItems(userid);
    }

    @ResponseBody
    @PostMapping("/pay")
    public void payController(HttpServletRequest request, HttpServletResponse response) throws IOException {
        AlipayClient alipayClient=new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.APP_ID, AlipayConfig.APP_PRIVATE_KEY,"json", AlipayConfig.CHARSET, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.sign_type);
        AlipayTradePagePayRequest alipayRequest=new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
        String out_trade_no=new String(request.getParameter("WIDout_trade_no").getBytes("ISO-8859-1"),"UTF-8");
        String total_amount=new String(request.getParameter("WIDtotal_amount").getBytes("ISO-8859-1"),"UTF-8");
        String subject=new String(request.getParameter("WIDsubject").getBytes("ISO-8859-1"),"UTF-8");
        String body=new String(request.getParameter("WIDbody").getBytes("ISO-8859-1"),"UTF-8");
        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}"
        );
        String form="";
        try{
            form=alipayClient.pageExecute(alipayRequest).getBody();
        }catch (AlipayApiException e){
            e.printStackTrace();
        }
        response.setContentType("text/html;charset="+ AlipayConfig.CHARSET);
        response.getWriter().write(form);
        response.getWriter().flush();
        response.getWriter().close();
    }

}
